﻿using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.UI;
using SouthernLand.Common;

namespace SouthernLand.WebMaster
{
    public partial class Photo : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string Action = Globals.GetStringFromQueryString("Action");

            if (Action == "Delete")
            {
                string ImageName = Globals.GetStringFromQueryString("Image");
                FileProcess.DeleteFile(Globals.PhysicalApplicationPath() + "/UserUpload/SlideShow/" + ImageName);
            }

            ItemsDataList.DataSource = ListUploadDirectory();
            ItemsDataList.DataBind();
        }

        public ICollection ListUploadDirectory()
        {
            var d = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/UserUpload/SlideShow"));
            return d.GetFileSystemInfos("*.jpg");
        }

        protected string FilePhoto_Upload()
        {
            string strFileName = "";
            // kiem tra xem user co chon hinh dai dien ko
            if (PhotoFile.HasFile)
            {
                // kiem tra phan mo rong cua file co hop le hay khong
                string strExtension = Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG")
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.Common.GetFileName() + Path.GetExtension(PhotoFile.FileName);

                    //duong dan tao save file Large
                    string strFilePathLarge = Request.PhysicalApplicationPath + "//UserUpload//SlideShow//" +
                                              strFileName;

                    //thuc hien save file Large len server
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, 234);

                    //Neu upload file Large thanh cong
                    if (uploadLarge == EnumUpload.UploadSucceed)
                    {
                        return strFileName;
                    }
                    else
                    {
                        // loi xay ra khi upload hinh
                        if (uploadLarge == EnumUpload.FileLarge)
                        {
                            // size toi da cho phep upload len server
                            int intPhotoSizeMax = Convert.ToInt32(ConfigurationManager.AppSettings["PhotoSizeMax"]);
                            lblMsg.CssClass = "error";
                            lblMsg.Text = "Bạn phải chọn hình có kích thước nhỏ hơn " + intPhotoSizeMax + " KB";
                            strFileName = "";
                        }
                    }
                }
                else
                {
                    lblMsg.CssClass = "error";
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    strFileName = "";
                }
            }

            return strFileName;
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (FilePhoto_Upload() != "")
            {
                lblMsg.CssClass = "lblMsg";
                lblMsg.Text = "Upload hình thành công!";

                ItemsDataList.DataSource = ListUploadDirectory();
                ItemsDataList.DataBind();
            }
        }
    }
}